package com.example.managesystem.controller.common;

import com.example.managesystem.chain.AbstractChainContext;
import com.example.managesystem.constant.JwtClaimsConstant;
import com.example.managesystem.context.BaseContext;
import com.example.managesystem.dto.AnnoPageQueryDTO;
import com.example.managesystem.vo.AnnoMaintextVO;
import com.example.managesystem.vo.AnnosVO;
import com.example.managesystem.dto.ResetPasswordDTO;
import com.example.managesystem.dto.UserLoginDTO;
import com.example.managesystem.entity.User;
import com.example.managesystem.properties.JwtProperties;
import com.example.managesystem.result.Result;
import com.example.managesystem.service.AnnoService;
import com.example.managesystem.service.UserService;
import com.example.managesystem.utils.JwtUtil;
import com.example.managesystem.vo.PageResult;
import com.example.managesystem.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@CrossOrigin(origins = "*") //origins可以指定请求来源，*代表全部
@Api(tags = "用户模块接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private AnnoService annoService;

    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private AbstractChainContext abstractChainContext;

    /**
     * 登录
     * @param userLoginDTO
     * @return
     */
    /*@CrossOrigin(origins = "http://localhost:8081")*/
    @PostMapping ("/login")
    @ApiOperation(value = "用户登录接口")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录请求：{}",userLoginDTO);

        User user =userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getUserid());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        /*log.info("jwt令牌：{}",token);*/
        //构建UserLoginVO返回对象 并为其中的name studentid token赋值
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .name(user.getName())
                .userid(user.getUserid())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }

    /**
     * 重置密码
     * @param resetPasswordDTO
     * @return
     */
    @PutMapping("/resetPassword")
    @ApiOperation(value ="重置密码")
    public Result resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO){
        log.info("重置密码请求：{}",resetPasswordDTO);
        userService.resetPassword(resetPasswordDTO);
        log.info("密码重置成功");
        return Result.success();
    }

    /**
     * 查询所有公告
     * @return
     */
    @GetMapping("/queryAllAnno")
  /*  @CrossOrigin(origins = "http://localhost:8081")*/
    @ApiOperation(value = "查询所有公告")
    public Result<AnnosVO> getAnnos(){
        log.info("查询所有公告11111111");
        AnnosVO annosVO=annoService.getAnnos();
        return Result.success(annosVO);
    }

    /**
     * 查询单个公告
     * @param id
     * @return
     */
    @GetMapping("quaryAnno")
    @ApiOperation(value = "查询单个公告")
    public Result<AnnoMaintextVO> getAnno(Integer id){
        log.info("查询公告id为{}",id);
        AnnoMaintextVO annoMaintextVO=annoService.getAnno(id);
        return Result.success(annoMaintextVO);
    }

    /**
     * 分页查询公告
     * @param annoPageQueryDTO
     * @return
     */
    @PostMapping("/pagequeryAnno")
    @ApiOperation(value = "分页查询公告")
    public Result<PageResult> pagequeryAnno(@RequestBody AnnoPageQueryDTO annoPageQueryDTO){
        log.info("分页查询公告请求：{}",annoPageQueryDTO);
        PageResult pageResult=annoService.pagequeryAnno(annoPageQueryDTO);
        return Result.success(pageResult);
    }


    /*@ApiOperation("获取当前用户id")
    @GetMapping("/user/info")
    public Result info(){

       *//* System.out.println((String) ThreadLocalUtil.get());*//*

        Long id=BaseContext.getCurrentId();
        String userid=String.valueOf(id);
        log.info("当前用户id：{}",userid);

        return null;
    }*/



}
